#coding=utf-8
import os
import re
import logging
from datiandou import settings

from django.core.cache import cache
import sys

reload(sys)
sys.setdefaultencoding('utf8')

import codecs
class Properties:
    LANGUAGE_FILE_DICT = dict([("zh-cn","message_CN.properties"),("en-us","message_EN.properties")])
    __KVDICT=dict()
    def __init__(self):
        logging.info('Properties INIT METHOD')


    def loadDiction(self, language):
        self.__KVDICT = dict()
        languageFile=self.getLanguageFile(language).readlines()
        print(languageFile)
        if languageFile is not None and len(languageFile)>0:
            for keyAndValueLine in languageFile:
                if(keyAndValueLine !=""):
                    keyAndValue=re.search(r'(.*)=(.*)', keyAndValueLine)
                    if keyAndValue:
                        keyAndValueGroups=keyAndValue.groups();
                        self.__KVDICT[keyAndValueGroups[0]]=keyAndValueGroups[1]
       # print(self.__KVDICT)
        return  self.__KVDICT

    def getLanguageFile(self,language):
        languageFileName=self.LANGUAGE_FILE_DICT.get(language)
        filePath=settings.PROJECT_PATH+str('datiandouweb/properties/')+languageFileName
        languageFile = codecs.open(filePath, encoding='utf-8',mode='r+')
       # print(languageFile)
       # print(language)
        return languageFile
    @staticmethod
    def getLanguageDict(metaaccept):
        accept=Properties.geti18nkey(metaaccept)
        accept= 'zh-cn' if accept is None else accept
        labelinfo=cache.get('messageinfo')
        if not labelinfo:
            prop = Properties()
            messageinfo = dict()
            languages=prop.LANGUAGE_FILE_DICT.keys()
            for language in languages:
                language_message = prop.loadDiction(language)
                messageinfo[language]=language_message
            cache.set('messageinfo',messageinfo,24*60*60)
            labelinfo=messageinfo
            #print(labelinfo)
        return labelinfo[accept]
    @staticmethod
    def geti18nkey(accept):
        i18nkey=None
        languages=Properties.LANGUAGE_FILE_DICT.keys()
        for language in languages:
            if accept in language:
                i18nkey=language
                break
        return i18nkey


    @staticmethod
    def getLanguageValueByKey(accept,key):
        accept=Properties.geti18nkey(accept)
        accept='zh-cn' if accept is None else accept
        labelinfo=cache.get('messageinfo')
        if not labelinfo:
            prop = Properties()
            messageinfo = dict()
            languages=prop.LANGUAGE_FILE_DICT.keys()
            for language in languages:
                language_message = prop.loadDiction(language)
                messageinfo[language]=language_message
            cache.set('messageinfo',messageinfo,24*60*60)
            labelinfo=messageinfo
            print(labelinfo)
        return labelinfo[accept].get(key)


#print(Properties.getLanguageValueByKey('CN','COMMON.LEFTMENU.TASKCENTER'))